<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="容器码" prop="containerCode" labelWidth="140rpx">
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.containerCode" 
					suffixIcon="scan"
					@confirm="bindRack"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="台车位码" prop="rackSlotCode" labelWidth="140rpx">
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.rackSlotCode"
					@confirm="bindRack"
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="料架码" prop="rackCode" labelWidth="140rpx" v-if="query.rackCode || query.rackCode !== ''">
				<u--input
					:disabled="true"
					placeholder="请输入/扫描"
					v-model="query.rackCode"
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<!-- 模态框 -->
		<u-modal
			:show="show"
			:title="title"
			:content='content'
			:showConfirmButton="true"
			:showCancelButton="false"
			@confirm="confirmSubmit"
		></u-modal>
		<!-- 提示组件 -->
		<u-toast ref="uToast"></u-toast>
		<view class="commonButton">
		  <!-- <u-button type="primary" @click="bindRack">确认绑定</u-button> -->
		  <u-button type="primary" :disabled="!query.rackCode" @click="queryQuality">质检抽料</u-button>
		  <u-button type="warning" @click="reset">重置</u-button>
		</view>
	</view>
</template>

<script>
	import { bindContainerAPI, callCarAPI, queryQualityAPI, handlerQualityAPI } from '@/api/work/inbound.js'
	export default {
		data() {
			return {
				query: {
					containerCode: '',
					rackSlotCode: '',
					rackCode: ''
				},
				// 模态框相关
				show: false,
				title: '提示',
				content: '物料不需要质检抽料，点击确认即可叫车，是否继续？'
			}
		},
		methods: {
			async bindRack() {
				if (!this.query.containerCode || !this.query.rackSlotCode) {
					return
				}
				const submitForm = {
					containerCode: this.query.containerCode,
					rackSlotCode: this.query.rackSlotCode
				}
				const res = await bindContainerAPI(submitForm)
				if (res.code === 200) {
					// 获取料架码
					this.$nextTick(() => {
						this.query.rackCode = res.data
					})
					this.query = {
						containerCode: '',
						rackSlotCode: ''
					}
					this.$refs.uToast.show({
						type: 'success',
						message: '绑定成功'
					})
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			async queryQuality() {
				// 调接口检测是否有待检物料
				const submitForm = {
					rackCode: this.query.rackCode
				}
				const res = await queryQualityAPI(submitForm)
				if (res.code === 200) {
					// if (res.data === false) {
					// 	const result = await handlerQualityAPI(submitForm)
					// 	if (result.code === 200) {
					// 		this.reset()
					// 		return this.$refs.uToast.show({
					// 			type: 'success',
					// 			message: result.message
					// 		})
					// 	} else {
					// 		return this.$refs.uToast.show({
					// 			type: 'warning',
					// 			message: result.message
					// 		})
					// 	}
					// } else {
					// 	this.show = true
					// }
					this.$refs.uToast.show({
						type: 'success',
						message: res.message
					})
					this.query = {
						containerCode: '',
						rackSlotCode: '',
						rackCode: ''
					}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			reset() {
				this.query = {
					containerCode: '',
					rackSlotCode: '',
					rackCode: ''
				}
			},
			async confirmSubmit() {
				// 没有待抽检物料触发叫车
				const submitForm = {
					rackCode: this.query.rackCode
				}
				const res = await callCarAPI(submitForm)
				if (res.code === 200) {
					this.show = false
					this.reset()
					this.$nextTick(() => {
						return this.$refs.uToast.show({
							type: 'success',
							message: res.message
						})
					})
				} else {
					this.show = false
					this.$nextTick(() => {
						return this.$refs.uToast.show({
							type: 'warning',
							message: res.message
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss">
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
}
</style>
